﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ecommerce2011Services.Models;
using System.Data.SqlClient;
using System.Data;

namespace ecommerce2011Services
{
    /// <summary>
    /// Descripción breve de Productos
    /// </summary>
    [WebService(Namespace = "http://192.168.0.27/WebService", Name = "Productos", Description = "Servicio Web de productos")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    // [System.Web.Script.Services.ScriptService]
    public class Productos : System.Web.Services.WebService
    {
        public static string DtsConnection = "Data Source=JHON-PC\\SQLEXPRESS;Initial Catalog=ProductosEcommerce;User ID=sa;Password=123456";

        SqlConnection Con = new SqlConnection(DtsConnection);

        

        [WebMethod]
        public Product getProducto(long productId)
        {
            Con.Open();

            SqlDataAdapter CMD = new SqlDataAdapter("Select * from Products where ProductId='" + productId + "'", Con);
            DataSet DS = new DataSet();

            CMD.Fill(DS, "DATOS");

            DataTable TablaProductos = DS.Tables[0];
            Product retorno = new Product();
            CityRate retornoCityRate = new CityRate();
            City retornoCity = new City();
            TransportationRate retornoTransportationRate = new TransportationRate();
            ShowRate retornoShowRate = new ShowRate();
            AccommodationRate retornoAccomRate = new AccommodationRate();


            retorno.ProductId = long.Parse(TablaProductos.Rows[0]["ProductId"].ToString());
            retorno.Name = TablaProductos.Rows[0]["Name"].ToString();
            retorno.Show = TablaProductos.Rows[0]["Show"].ToString();
            retorno.ShowDate = DateTime.Parse(TablaProductos.Rows[0]["ShowDate"].ToString());
            retorno.ArrivalDate = DateTime.Parse(TablaProductos.Rows[0]["ArrivalDate"].ToString());
            retorno.DepartureDate = DateTime.Parse(TablaProductos.Rows[0]["DepartureDate"].ToString());
            retorno.Image = TablaProductos.Rows[0]["Image"].ToString();
            ///////

            SqlDataAdapter cmdCity = new SqlDataAdapter("Select * from Cities where CityId='" + (TablaProductos.Rows[0]["CityId"].ToString()) + "'", Con);
            DataSet dsCity = new DataSet();
            cmdCity.Fill(dsCity, "DATOS");
            DataTable TablaCiudad = dsCity.Tables[0];

            SqlDataAdapter cmdCityRate = new SqlDataAdapter("Select * from CityRates where CityRateId='" + (TablaCiudad.Rows[0]["CityRateId"].ToString()) + "'", Con);
            DataSet dsCityRate = new DataSet();
            cmdCityRate.Fill(dsCityRate, "DATOS");
            DataTable TablaCiudadTarifa = dsCityRate.Tables[0];

            SqlDataAdapter cmdShow = new SqlDataAdapter("Select * from ShowRates where ShowRateId='" + (TablaProductos.Rows[0]["ShowRateId"].ToString()) + "'", Con);
            DataSet dsShow = new DataSet();
            cmdShow.Fill(dsShow, "DATOS");
            DataTable TablaEspecta = dsShow.Tables[0];

            retornoCityRate.CityRateId = long.Parse(TablaCiudadTarifa.Rows[0]["CityRateId"].ToString());
            retornoCityRate.NameRate = TablaCiudadTarifa.Rows[0]["NameRate"].ToString();
            retornoCityRate.Price = decimal.Parse(TablaCiudadTarifa.Rows[0]["Price"].ToString());

            retornoCity.CityId = TablaCiudad.Rows[0]["CityId"].ToString();
            retornoCity.Name = TablaCiudad.Rows[0]["Name"].ToString();
            retornoCity.Country = TablaCiudad.Rows[0]["Country"].ToString();
            retornoCity.CityRate = retornoCityRate;
            retorno.City = retornoCity;

            retornoShowRate.ShowRateId = long.Parse(TablaEspecta.Rows[0]["ShowRateId"].ToString());
            retornoShowRate.NameRate = TablaEspecta.Rows[0]["NameRate"].ToString();
            retornoShowRate.Price = decimal.Parse(TablaEspecta.Rows[0]["Price"].ToString());
            retorno.ShowRate = retornoShowRate;

            if (TablaProductos.Rows[0]["TransportationRateId"].ToString() != null && !TablaProductos.Rows[0]["TransportationRateId"].ToString().Equals(""))
            {
                SqlDataAdapter cmdTransp = new SqlDataAdapter("Select * from TransportationRates where TransportationRateId='" + (TablaProductos.Rows[0]["TransportationRateId"].ToString()) + "'", Con);
                DataSet dsTransp = new DataSet();
                cmdTransp.Fill(dsTransp, "DATOS");
                DataTable TablaTransp = dsTransp.Tables[0];

                retornoTransportationRate.TransportationRateId = long.Parse(TablaTransp.Rows[0]["TransportationRateId"].ToString());
                retornoTransportationRate.NameRate = TablaTransp.Rows[0]["NameRate"].ToString();
                retornoTransportationRate.Price = decimal.Parse(TablaTransp.Rows[0]["Price"].ToString());
            }

            if (TablaProductos.Rows[0]["AccommodationRateId"].ToString() != null && !TablaProductos.Rows[0]["AccommodationRateId"].ToString().Equals(""))
            {
                SqlDataAdapter cmdAccom = new SqlDataAdapter("Select * from AccommodationRates where AccommodationRateId='" + (TablaProductos.Rows[0]["AccommodationRateId"].ToString()) + "'", Con);
                DataSet dsAccom = new DataSet();
                cmdAccom.Fill(dsAccom, "DATOS");
                DataTable TablaAccom = dsAccom.Tables[0];

                retornoAccomRate.AccommodationRateId = long.Parse(TablaAccom.Rows[0]["AccommodationRateId"].ToString());
                retornoAccomRate.NameRate = TablaAccom.Rows[0]["NameRate"].ToString();
                retornoAccomRate.Price = decimal.Parse(TablaAccom.Rows[0]["Price"].ToString());
            }

            retorno.TransportationRate = retornoTransportationRate;
            retorno.AccommodationRate = retornoAccomRate;

            return retorno;
        }
    }
}